//
//  UIDevice+XLDeviceModel.m
//  XLDeviceModel
//
//  Created by ilongge on 2022/5/13.
//

#import "UIDevice+XLDeviceModel.h"
#import <sys/utsname.h>

@implementation UIDevice (XLDeviceModel)

+ (NSString *)xl_currentDeviceModelDescription {
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *platform = [NSString stringWithCString:systemInfo.machine encoding:NSASCIIStringEncoding];
    return platform;
}

// 获取设备型号然后手动转化为对应名称
+ (NSString *)xl_currentDeviceModel {
    NSDictionary *device_info_dict = [self xl_devicePlatformMap];
    NSString *platform = [UIDevice xl_currentDeviceModelDescription];
    NSString *device_name = [device_info_dict objectForKey:platform];
    if (device_name == nil) {
        device_name = @"Unknow Device Model";
    }
    return device_name;
}

+ (NSDictionary *)xl_devicePlatformMap
{
    NSMutableDictionary *map = [NSMutableDictionary dictionary];
    [map addEntriesFromDictionary:[self xl_tvPlatformMap]];
    [map addEntriesFromDictionary:[self xl_watchPlatformMap]];
    [map addEntriesFromDictionary:[self xl_iPodPlatformMap]];
    [map addEntriesFromDictionary:[self xl_iPadPlatformMap]];
    [map addEntriesFromDictionary:[self xl_iPhonePlatformMap]];
    [map addEntriesFromDictionary:[self xl_simulatorPlatformMap]];
    return [NSDictionary dictionaryWithDictionary:map];
}

+ (NSDictionary *)xl_tvPlatformMap
{
    NSDictionary *map =  @{
        @"AppleTV2,1" : @"Apple TV Gen 2",
        @"AppleTV3,1" : @"Apple TV Gen 3",
        @"AppleTV3,2" : @"Apple TV Gen 3",
        @"AppleTV5,3" : @"Apple TV Gen 4",
        @"AppleTV6,2" : @"Apple TV 4k Gen 1",
        @"AppleTV11,1" : @"Apple TV 4k Gen 2",
        @"AppleTV14,1" : @"Apple TV 4k Gen 3"
    };
    return map;
}

+ (NSDictionary *)xl_iPodPlatformMap
{
    NSDictionary *map = @{
        @"iPod1,1" : @"iPod Touch",
        @"iPod2,1" : @"iPod Touch 2",
        @"iPod3,1" : @"iPod Touch 3",
        @"iPod4,1" : @"iPod Touch 4",
        @"iPod5,1" : @"iPod Touch 5",
        @"iPod7,1" : @"iPod Touch 6",
        @"iPod9,1" : @"iPod Touch 7"
    };
    return map;
}

+ (NSDictionary *)xl_watchPlatformMap
{
    NSDictionary *map =  @{
        @"Watch1,1" : @"Apple Watch(38mm)",
        @"Watch1,2" : @"Apple Watch(42mm)",
        @"Watch2,3" : @"Apple Watch Series 2(38mm)",
        @"Watch2,4" : @"Apple Watch Series 2(42mm)",
        @"Watch2,6" : @"Apple Watch Series 1(38mm)",
        @"Watch2,7" : @"Apple Watch Series 1(42mm)",
        @"Watch3,1" : @"Apple Watch Series 3(38mm)",
        @"Watch3,2" : @"Apple Watch Series 3(42mm)",
        @"Watch3,3" : @"Apple Watch Series 3(38mm)",
        @"Watch3,4" : @"Apple Watch Series 3(42mm)",
        @"Watch4,1" : @"Apple Watch Series 4(40mm)",
        @"Watch4,2" : @"Apple Watch Series 4(44mm)",
        @"Watch4,3" : @"Apple Watch Series 4(40mm)",
        @"Watch4,4" : @"Apple Watch Series 4(44mm)",
        @"Watch5,1" : @"Apple Watch Series 5(40mm)",
        @"Watch5,2" : @"Apple Watch Series 5(44mm)",
        @"Watch5,3" : @"Apple Watch Series 5(40mm)",
        @"Watch5,4" : @"Apple Watch Series 5(44mm)",
        @"Watch5,9" : @"Apple Watch SE Gen 1(40mm)",
        @"Watch5,10" : @"Apple Watch SE Gen 1(44mm)",
        @"Watch5,11" : @"Apple Watch SE Gen 1(40mm)",
        @"Watch5,12" : @"Apple Watch SE Gen 1(44mm)",
        @"Watch6,1" : @"Apple Watch Series 6(40mm)",
        @"Watch6,2" : @"Apple Watch Series 6(44mm)",
        @"Watch6,3" : @"Apple Watch Series 6(40mm)",
        @"Watch6,4" : @"Apple Watch Series 6(44mm)",
        @"Watch6,6" : @"Apple Watch Series 7(41mm)",
        @"Watch6,7" : @"Apple Watch Series 7(45mm)",
        @"Watch6,8" : @"Apple Watch Series 7(41mm)",
        @"Watch6,9" : @"Apple Watch Series 7(45mm)",
        @"Watch6,10" : @"Apple Watch SE Gen 2(41mm)",
        @"Watch6,11" : @"Apple Watch SE Gen 2(45mm)",
        @"Watch6,12" : @"Apple Watch SE Gen 2(41mm)",
        @"Watch6,13" : @"Apple Watch SE Gen 2(45mm)",
        @"Watch6,14" : @"Apple Watch Series 8(41mm)",
        @"Watch6,15" : @"Apple Watch Series 8(45mm)",
        @"Watch6,16" : @"Apple Watch Series 8(41mm)",
        @"Watch6,17" : @"Apple Watch Series 8(45mm)",
        @"Watch6,18" : @"Apple Watch Ultra(49mm)"
    };
    return map;
}

+ (NSDictionary *)xl_iPadPlatformMap
{
    NSDictionary *map =  @{
        @"iPad1,1" : @"iPad 1",
        @"iPad1,2" : @"iPad 3G",
        @"iPad2,1" : @"iPad 2",
        @"iPad2,2" : @"iPad 2",
        @"iPad2,3" : @"iPad 2",
        @"iPad2,4" : @"iPad 2",
        @"iPad2,5" : @"iPad Mini",
        @"iPad2,6" : @"iPad Mini",
        @"iPad2,7" : @"iPad Mini",
        @"iPad3,1" : @"iPad 3",
        @"iPad3,2" : @"iPad 3",
        @"iPad3,3" : @"iPad 3",
        @"iPad3,4" : @"iPad 4",
        @"iPad3,5" : @"iPad 4",
        @"iPad3,6" : @"iPad 4",
        @"iPad4,1" : @"iPad Air",
        @"iPad4,2" : @"iPad Air",
        @"iPad4,3" : @"iPad Air",
        @"iPad4,4" : @"iPad Mini 2",
        @"iPad4,5" : @"iPad Mini 2",
        @"iPad4,6" : @"iPad Mini 2",
        @"iPad4,7" : @"iPad Mini 3",
        @"iPad4,8" : @"iPad Mini 3",
        @"iPad4,9" : @"iPad Mini 3",
        @"iPad5,1" : @"iPad Mini 4",
        @"iPad5,2" : @"iPad Mini 4",
        @"iPad5,3" : @"iPad Air 2",
        @"iPad5,4" : @"iPad Air 2",
        @"iPad6,3" : @"iPad Pro(9.7)",
        @"iPad6,4" : @"iPad Pro(9.7)",
        @"iPad6,7" : @"iPad Pro(12.9)",
        @"iPad6,8" : @"iPad Pro(12.9)",
        @"iPad6,11" : @"iPad 6",
        @"iPad6,12" : @"iPad 6",
        @"iPad7,1" : @"iPad Pro 2(12.9)",
        @"iPad7,2" : @"iPad Pro 2(12.9)",
        @"iPad7,3" : @"iPad Pro 2(10.5)",
        @"iPad7,4" : @"iPad Pro 2(10.5)",
        @"iPad7,5" : @"iPad 2018",
        @"iPad7,6" : @"iPad 2018",
        @"iPad7,11" : @"iPad 2019",
        @"iPad7,12" : @"iPad 2019",
        @"iPad8,1" : @"iPad Pro 3(11)",
        @"iPad8,2" : @"iPad Pro 3(11)",
        @"iPad8,3" : @"iPad Pro 3(11)",
        @"iPad8,4" : @"iPad Pro 3(11)",
        @"iPad8,5" : @"iPad Pro 3(12.9)",
        @"iPad8,6" : @"iPad Pro 3(12.9)",
        @"iPad8,7" : @"iPad Pro 3(12.9)",
        @"iPad8,8" : @"iPad Pro 3(12.9)",
        @"iPad8,9" : @"iPad Pro 4(11)",
        @"iPad8,10" : @"iPad Pro 4(11)",
        @"iPad8,11" : @"iPad Pro 4(12.9)",
        @"iPad8,12" : @"iPad Pro 4(12.9)",
        @"iPad11,1" : @"iPad Mini 5",
        @"iPad11,2" : @"iPad Mini 5",
        @"iPad11,3" : @"iPad Air 3",
        @"iPad11,4" : @"iPad Air 3",
        @"iPad11,6" : @"iPad 8",
        @"iPad11,7" : @"iPad 8",
        @"iPad12,1" : @"iPad 9",
        @"iPad12,2" : @"iPad 9",
        @"iPad13,1" : @"iPad Air 4",
        @"iPad13,2" : @"iPad Air 4",
        @"iPad13,4" : @"iPad Pro 5(11)",
        @"iPad13,5" : @"iPad Pro 5(11)",
        @"iPad13,6" : @"iPad Pro 5(11)",
        @"iPad13,7" : @"iPad Pro 5(11)",
        @"iPad13,8" : @"iPad Pro 5(12.9)",
        @"iPad13,9" : @"iPad Pro 5(12.9)",
        @"iPad13,10" : @"iPad Pro 5(12.9)",
        @"iPad13,11" : @"iPad Pro 5(12.9)",
        @"iPad13,16" : @"iPad Air 5",
        @"iPad13,17" : @"iPad Air 5",
        @"iPad14,1" : @"iPad Mini 6",
        @"iPad14,2" : @"iPad Mini 6",
        @"iPad14,3" : @"iPad Pro 6(11)",
        @"iPad14,4" : @"iPad Pro 6(11)",
        @"iPad14,5" : @"iPad Pro 6(12.9)",
        @"iPad14,6" : @"iPad Pro 6(12.9)"
    };
    return map;
}

+ (NSDictionary *)xl_iPhonePlatformMap
{
    NSDictionary *map = @{
        @"iPhone3,1" : @"iPhone 4",
        @"iPhone3,2" : @"iPhone 4",
        @"iPhone3,3" : @"iPhone 4",
        @"iPhone4,1" : @"iPhone 4S",
        @"iPhone5,1" : @"iPhone 5",
        @"iPhone5,2" : @"iPhone 5",
        @"iPhone5,3" : @"iPhone 5C",
        @"iPhone5,4" : @"iPhone 5C",
        @"iPhone6,1" : @"iPhone 5S",
        @"iPhone6,2" : @"iPhone 5S",
        @"iPhone7,1" : @"iPhone 6 Plus",
        @"iPhone7,2" : @"iPhone 6",
        @"iPhone8,1" : @"iPhone 6S",
        @"iPhone8,2" : @"iPhone 6S Plus",
        @"iPhone8,4" : @"iPhone SE",
        @"iPhone9,1" : @"iPhone 7",
        @"iPhone9,2" : @"iPhone 7 Plus",
        @"iPhone9,3" : @"iPhone 7",
        @"iPhone9,4" : @"iPhone 7 Plus",
        @"iPhone10,1" : @"iPhone 8",
        @"iPhone10,2" : @"iPhone 8 Plus",
        @"iPhone10,3" : @"iPhone X",
        @"iPhone10,4" : @"iPhone 8",
        @"iPhone10,5" : @"iPhone 8 Plus",
        @"iPhone10,6" : @"iPhone X",
        @"iPhone11,2" : @"iPhone XS",
        @"iPhone11,4" : @"iPhone XS Max",
        @"iPhone11,6" : @"iPhone XS Max",
        @"iPhone11,8" : @"iPhone XR",
        @"iPhone12,1" : @"iPhone 11",
        @"iPhone12,3" : @"iPhone 11 Pro",
        @"iPhone12,5" : @"iPhone 11 Pro Max",
        @"iPhone12,8" : @"iPhone SE 2",
        @"iPhone13,1" : @"iPhone 12 Mini",
        @"iPhone13,2" : @"iPhone 12",
        @"iPhone13,3" : @"iPhone 12 Pro",
        @"iPhone13,4" : @"iPhone 12 Pro Max",
        @"iPhone14,2" : @"iPhone 13 Pro",
        @"iPhone14,3" : @"iPhone 13 Pro Max",
        @"iPhone14,4" : @"iPhone 13 Mini",
        @"iPhone14,5" : @"iPhone 13",
        @"iPhone14,6" : @"iPhone SE 3",
        @"iPhone14,7" : @"iPhone 14",
        @"iPhone14,8" : @"iPhone 14 Plus",
        @"iPhone15,2" : @"iPhone 14 Pro",
        @"iPhone15,3" : @"iPhone 14 Pro Max",
    };
    return map;
}
+ (NSDictionary *)xl_simulatorPlatformMap
{
    NSDictionary *map = @{
        @"x86_64" : @"Simulator",
        @"arm64" : @"Simulator",
        @"i386" : @"Simulator"
    };
    return map;
}
@end
